package com.ez.supervise.action.biz;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.ServletActionContext;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.OrgCheck;
import com.ez.supervise.service.OrgCheckService;
import com.ez.supervise.util.JsonUtils;

public class OrgCheckAction extends BaseJsonAction {

    private static final long serialVersionUID = 585976345704483184L;
    private Long id;
    private OrgCheckService orgCheckService;
    private OrgCheck orgCheck;
    private Page page;
   

    public String saveOrgCheck() {
	try {
	    if (orgCheck.isNew()) {
		orgCheckService.insert(orgCheck);
	    } else {
		orgCheckService.update(orgCheck);
	    }
	    Result result = new Result(orgCheck.getId());
	    result.setEntity(orgCheck);
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new",
	    "content" });
	    outString(JSONObject.fromObject(result, config).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    
    public String writeOrgCheck() {
	try {
	    if (orgCheck.isNew()) {
		//
	    } else {
		//set value
		orgCheck.setCheckStatus(OrgCheck.CHECK_STATUS_SELF);
		orgCheckService.update(orgCheck);
	    }
	    Result result = new Result(orgCheck.getId());
	    result.setEntity(orgCheck);
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new",
		"content" });
	    outString(JSONObject.fromObject(result, config).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String auditOrgCheck() {
	try {
	    if (orgCheck.isNew()) {
		//
	    } else {
		//set value
		orgCheck.setCheckStatus(OrgCheck.CHECK_STATUS_AUDIT);
		orgCheckService.update(orgCheck);
	    }
	    Result result = new Result(orgCheck.getId());
	    result.setEntity(orgCheck);
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new",
		"content" });
	    outString(JSONObject.fromObject(result, config).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    
    public String findAllOrgCheck() {
	try {
	    page = new Page();
	    List result = orgCheckService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findOrgCheckSelf() {
	try {
	    page = new Page();
	    page.putCondition("year", NumberUtils.isNumber(getRequest().getParameter("year")) ? Long.valueOf(getRequest().getParameter("year")) : null);
	    page.putCondition("content",getRequest().getParameter("content"));
	    if(isLogin()) {
		page.putCondition("orgId", getLoginUser().getOrgId());
	    } else {
		page.putCondition("orgId", 0L);
	    }
	    int start = NumberUtils.toInt(getRequest().getParameter("start"), 0);
	    int limit = NumberUtils.toInt(getRequest().getParameter("limit"), 20);
	    page.setStart(start++);
	    page.setLimit(limit);
	    page.addPageInfo();
	    orgCheckService.findOrgCheckSelfByPage(page);
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    
    public String findOrgCheckAuditByPage() {
	try {
	    page = new Page();
	    page.putCondition("year", NumberUtils.isNumber(getRequest().getParameter("year")) ? Long.valueOf(getRequest().getParameter("year")) : null);
	    page.putCondition("orgId", NumberUtils.isNumber(getRequest().getParameter("orgId")) ? Long.valueOf(getRequest().getParameter("orgId")) : null);
	    if(isLogin()) {
		page.putCondition("checkOrgId", getLoginUser().getOrgId());
	    } else {
		page.putCondition("checkOrgId", 0L);
	    }
	    int start = NumberUtils.toInt(getRequest().getParameter("start"), 0);
	    int limit = NumberUtils.toInt(getRequest().getParameter("limit"), 20);
	    page.setStart(start++);
	    page.setLimit(limit);
	    page.addPageInfo();
	    orgCheckService.findOrgCheckAuditByPage(page);
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    
    
    public String findAll() {
	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(orgCheckService.findAll());

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }
  
    public String findOrgCheckByPage(){
	try {
	    page = new Page();
	    page.putCondition("year", NumberUtils.isNumber(getRequest().getParameter("year")) ? Long.valueOf(getRequest().getParameter("year")) : null);
	    page.putCondition("orgId", NumberUtils.isNumber(getRequest().getParameter("orgId")) ? Long.valueOf(getRequest().getParameter("orgId")) : null);
	    int start = NumberUtils.toInt(getRequest().getParameter("start"), 0);
	    int limit = NumberUtils.toInt(getRequest().getParameter("limit"), 20);
	    page.setStart(start++);
	    page.setLimit(limit);
	    page.addPageInfo();
	    orgCheckService.findOrgCheckByPage(page);
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    
    public String findGoalByType() {
	String type = getRequest().getParameter("type");
	if (!OrgCheck.TYPE_SELF.equals(type)) {
	    type = OrgCheck.TYPE_NORMAL;
	}

	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(orgCheckService.findGoalByType(type));

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }
    /**
     * @return
     */
    public String deleteOrgCheck() {
	try {
	    if (id != null && id > 0) {
		orgCheckService.delete(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateOrgCheck() {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
//	String strOrgCheckId = getRequest().getParameter("id");
	try {
	    if (id != null && id > 0) {
		OrgCheck saveOrgCheck = new OrgCheck();
		saveOrgCheck.setId(id);
		BeanUtils.setProperty(saveOrgCheck, fieldName, fieldValue);
		orgCheckService.update(saveOrgCheck);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public static void main(String[] args) {
	OrgCheck orgCheck = new OrgCheck();
	orgCheck.setId(1L);
	// orgCheck.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(orgCheck);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(orgCheck, config);
	System.out.println(jsonObj.toString());
    }

    public void setOrgCheckService(OrgCheckService orgCheckService) {
	this.orgCheckService = orgCheckService;
    }

    public OrgCheck getOrgCheck() {
	return orgCheck;
    }

    public void setOrgCheck(OrgCheck orgCheck) {
	this.orgCheck = orgCheck;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

}
